package com.jdzy.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdzy.model.common.vos.ResultA;
import com.jdzy.model.student.dtos.StudentTagDTO;
import com.jdzy.model.student.dtos.TagDTO;
import com.jdzy.model.student.pojos.StudentTagRelation;
import com.jdzy.model.student.pojos.Tag;
import com.jdzy.student.mapper.StudentTagRelationMapper;
import com.jdzy.student.mapper.TagMapper;
import com.jdzy.student.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private StudentTagRelationMapper studentTagRelationMapper;

    @Override
    public ResultA<StudentTagDTO> getStudentTagsByStuId(String stuId) {
        List<StudentTagRelation> relations = studentTagRelationMapper.getStudentTagRelationsByStuId(stuId);
        if (relations.isEmpty()) {
            return ResultA.ok(new StudentTagDTO(stuId, Collections.emptyList()));
        }

        List<Long> tagIds = relations.stream().map(StudentTagRelation::getTagId).collect(Collectors.toList());
        List<TagDTO> tagDTOs = tagMapper.selectBatchIds(tagIds).stream()
                .map(tag -> {
                    TagDTO tagDTO = tagMapper.getTagDTOById(tag.getId());
                    return tagDTO;
                }).collect(Collectors.toList());

        StudentTagDTO studentTagDTO = new StudentTagDTO();
        studentTagDTO.setStuId(stuId);
        studentTagDTO.setTags(tagDTOs);

        return ResultA.ok(studentTagDTO);

    }

    @Override
    public ResultA<Tag> saveOrUpdateTag(TagDTO tagDTO) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagDTO, tag);

        // 判断是新增还是修改
        if (tag.getId() == null) {
            // 新增标签
            this.save(tag);

            // 新增学生标签关系
            StudentTagRelation studentTagRelation = new StudentTagRelation();
            studentTagRelation.setStuId(tagDTO.getStuId());
            studentTagRelation.setTagId(tag.getId());
            studentTagRelationMapper.insert(studentTagRelation);
        } else {
            // 修改标签
            // 先删除原有的学生标签关系
            QueryWrapper<StudentTagRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tag_id", tag.getId());
            studentTagRelationMapper.delete(queryWrapper);

            // 更新标签信息
            this.updateById(tag);

            // 新增更新后的学生标签关系
            StudentTagRelation studentTagRelation = new StudentTagRelation();
            studentTagRelation.setStuId(tagDTO.getStuId());
            studentTagRelation.setTagId(tag.getId());
            studentTagRelationMapper.insert(studentTagRelation);
        }

        return ResultA.ok(tag);
    }

    @Override
    public ResultA<Void> deleteTag(Long tagId, String stuId) {
        // 删除学生标签关系
        studentTagRelationMapper.deleteStudentTagRelation(stuId, tagId);

        // 根据需求，可以选择是否同时删除标签本身
         this.removeById(tagId); // 如果需要删除标签本身，则取消注释

        return ResultA.ok();
    }


}
